

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// new JettyServer(path, 8080, "").start();
public class JettyServer {
	private Logger logger = LoggerFactory.getLogger(getClass());

	private Object lock = new Object();
	private Server instance;
	

	private int port = 8080;
	private String configLocation;

	public void start() {

		synchronized (lock) {
			if (null == instance) {
				instance = initInstance();
			}
		}

		try {
			logger.warn("Prepare start RestServer on port "+port+"..", port);
			instance.start();
			// instance.join();
			logger.warn("Start JettyServer on port "+port+" success!", port);
		} catch (Exception e) {
			logger.error("Start JettyServer on port "+port+" fail!", port);
			throw new RuntimeException(e);
		}
	}

	private Server initInstance() {
//		WebContext handler = new ServletContextHandler();
		WebAppContext handler = new WebAppContext();
			
			
		handler.setContextPath("/proxyip");

//		ServletHolder holder = new ServletHolder("cxf-servlet",CXFServlet.class);//rest
//		holder.setInitParameter("config-location", configLocation);
//		handler.addServlet(holder, "/*");
//		handler.setDescriptor("D:/workspace_oss2/proxyip/proxyip-web/src/main/webapp/WEB-INF/web.xml");  
//		handler.setResourceBase("D:/workspace_oss2/proxyip/proxyip-web/src/main/webapp");  
		handler.setDescriptor("/opt/jetty/proxyip/WEB-INF/web.xml");  
		handler.setResourceBase("/opt/jetty/proxyip"); 
//		handler.setDisplayName("proxyip");
		
		handler.setClassLoader(Thread.currentThread().getContextClassLoader());  //定位class文件位置的。
		
		handler.setConfigurationDiscovered(true);  
		handler.setParentLoaderPriority(true);  
		

		SelectChannelConnector connector = new SelectChannelConnector();
		connector.setName("Jetty-Selector");
		connector.setPort(port);
		
		Server server = new Server();
		server.addConnector(connector);
		server.setHandler(handler);
		return server;
	}

	public void stop() {
		if (null != instance) {
			try {
				instance.stop();
			} catch (Exception e) {
				logger.error("stop JettyServer occurs error.", e);
			}
		}
	}
	
	public static void main(String[] args) {
		new JettyServer().start();
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getConfigLocation() {
		return configLocation;
	}

	public void setConfigLocation(String configLocation) {
		this.configLocation = configLocation;
	}
}
